package com.github.dice.service.impl;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.dice.dto.RollDiceDTO;
import com.github.dice.entity.Player;
import com.github.dice.entity.PlayerAndDiceMapping;
import com.github.dice.entity.Room;
import com.github.dice.mapper.PlayerMapper;
import com.github.dice.service.IPlayerAndDiceMappingService;
import com.github.dice.service.IPlayerService;
import com.github.dice.service.IRoomService;

@Service
public class PlayerService extends ServiceImpl<PlayerMapper, Player> implements IPlayerService {

    @Resource
    private PlayerMapper                 playerMapper;

    @Resource
    private IRoomService                 roomService;

    @Resource
    private IPlayerAndDiceMappingService playerAndDiceMappingService;

    public Player getPlayByNameAndPwd(@Nonnull String playerName, @Nonnull String playerPwd) {
        QueryWrapper<Player> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("player_name", playerName);
        queryWrapper.eq("player_pwd", playerPwd);
        queryWrapper.eq("is_delete", 0);
        Player player = playerMapper.selectOne(queryWrapper);
        if (null != player) {
            player.setPlayerPwd("");
        }
        return player;
    }

    public Player getPlayerByName(@Nonnull String playerName) {
        QueryWrapper<Player> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("player_name", playerName);
        queryWrapper.eq("is_delete", 0);
        Player player = playerMapper.selectOne(queryWrapper);
        if (null != player) {
            player.setPlayerPwd("");
            return player;
        } else {
            return null;
        }
    }

    public Player addPlayer(Player player) {
        Player findPlay = getPlayerByName(player.getPlayerName());
        if (ObjectUtils.isEmpty(findPlay)) {
            player.setIsDelete(0);
            playerMapper.insert(player);
            return player;
        } else {
            return findPlay;
        }
    }

    public void rollDice(RollDiceDTO rollDiceDTO) {
        // check player in the room
        PlayerAndDiceMapping item ;
        item = playerAndDiceMappingService.getPlayerAndDiceMappingListByRoomAndPlayer(rollDiceDTO.getRoomNumber(),
                rollDiceDTO.getPlayerName());
        // get room
        Room room = roomService.getRoomByNumber(rollDiceDTO.getRoomNumber());
        //get player
        Player player = getPlayerByName(rollDiceDTO.getPlayerName());
        // save playerAndDiceMapping
        if (ObjectUtils.isEmpty(item)) {
            item = new PlayerAndDiceMapping();
            item.setRoomId(room.getRoomId());
            item.setPlayerId(player.getPlayerId());
            item.setDiceNumber(rollDiceDTO.getDiceNumber());
            playerAndDiceMappingService.addPlayerAndDiceMapping(item);
        } else {
            item.setRoomId(room.getRoomId());
            item.setPlayerId(player.getPlayerId());
            item.setDiceNumber(rollDiceDTO.getDiceNumber());
            playerAndDiceMappingService.updatePlayerDice(item);
        }
    }
}
